var user_reply_apis = {
	replyList: "http://api.fangchepingtai.com/fangchebbs/user/reply/list",
	replyCount: "http://api.fangchepingtai.com/fangchebbs/user/reply/count",
	replyDelete: "http://api.fangchepingtai.com/fangchebbs/user/reply/delete/",
};
(function() {
	var page, num, pages;

	// 获取用户回复列表
	function getUserReplyList(page, num) {
		coAjax.get(user_reply_apis.replyList, {
			page: page,
			num: num,
			creater_id: App.get("loginer._id")
		}, function(result) {
			console.log(result);
			App.set("$Cache.user.reply.list", result);
		}, function(err, errMsg, errCode) {
			console.log(err);
			console.log(errMsg);
		});
	};

	// 删除回复
	App.set("$Event.user.reply.delete", function(e, vm) {
		myConfirm("确定要删除此回复吗？删除后不可恢复", function() {
			coAjax.delete(user_reply_apis.replyDelete + vm.get("_id"), function(result) {
				vm.remove();
				alert("success", "删除成功");
			}, function(err, errMsg, errCode) {
				console.log(err);
				console.log(errMsg);
			});
		});
	});

	// 翻页
	App.set("$Event.user.reply.prePage", function() {
		var jumpPage = page;
		jumpPage > 0 ? jumpPage-- : jumpPage = 0;
		Path.jump("/user/reply.html?page=" + jumpPage + "&num=" + num);
	});
	App.set("$Event.user.reply.nextPage", function() {
		var jumpPage = page;
		jumpPage < pages.length - 1 ? jumpPage++ : jumpPage = pages.length - 1;
		Path.jump("/user/reply.html?page=" + jumpPage + "&num=" + num);
	});


	Path.on("/user/__basename__.html", function(_current_location) {
		var qs = _current_location.query;
		page = qs.get("page") || 0;
		App.set("$Cache.user.reply.current_page", page);
		num = qs.get("num") || 15;
		App.set("$Cache.user.reply.page_num", num);
		getUserReplyList(page, num);

		// 获取用户回复列表统计，分页
		coAjax.get(user_reply_apis.replyCount, {
			creater_id: App.get("loginer._id")
		}, function(result) {
			// console.log(result);
			pages = [];
			pages.length = Math.ceil(result / num) || 1;
			App.set("$Cache.user.reply.pages", pages);
		}, function(err, errMsg, errCode) {
			console.log(err);
			console.log(errMsg);
		});
	});
}());